﻿#include "passgensettingdialog.h"
#include "ui_passgensettingdialog.h"
#include <QSettings>
#include <QDebug>
#include "flatui.h"

PassGenSettingDialog::PassGenSettingDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::PassGenSettingDialog)
{
    ui->setupUi(this);
    loadQss();

    loadGenSetting();

    connect(ui->length_horizontalSlider,&QSlider::valueChanged,[=](){
       ui->length_show_label->setText(tr("%1").arg(ui->length_horizontalSlider->value()));
    });
    connect(ui->xxzm_horizontalSlider,&QSlider::valueChanged,[=](){
        ui->xxzm_show_label->setText(tr("%1").arg(ui->xxzm_horizontalSlider->value()));
    });
    connect(ui->dxzm_horizontalSlider,&QSlider::valueChanged,[=](){
        ui->dxzm_show_label->setText(tr("%1").arg(ui->dxzm_horizontalSlider->value()));
    });
    connect(ui->num_horizontalSlider,&QSlider::valueChanged,[=](){
        ui->num_show_label->setText(tr("%1").arg(ui->num_horizontalSlider->value()));
    });
    connect(ui->tsfh_horizontalSlider,&QSlider::valueChanged,[=](){
        ui->tsfh_show_label->setText(tr("%1").arg(ui->tsfh_horizontalSlider->value()));
    });
}

PassGenSettingDialog::~PassGenSettingDialog()
{
    delete ui;
}

void PassGenSettingDialog::on_ok_pushButton_clicked()
{
    QSettings settings;
    //int wrap = settings.value("editor/wrapMargin").toInt();
    //小写字母
    settings.setValue("passgen/xxzm",ui->xxzm_horizontalSlider->value());
    //大写字母
    settings.setValue("passgen/dxzm",ui->dxzm_horizontalSlider->value());
    //数字
    settings.setValue("passgen/num",ui->num_horizontalSlider->value());
    //特殊符号
    settings.setValue("passgen/tsfh",ui->tsfh_horizontalSlider->value());
    //排除字符
    settings.setValue("passgen/excludeChar",ui->exclude_lineEdit->text());
    //密码长度
    settings.setValue("passgen/passlen",ui->length_horizontalSlider->value());

    close();
    //qDebug()<<wrap;
}

void PassGenSettingDialog::loadGenSetting()
{
    QSettings settings;
    ui->xxzm_horizontalSlider->setValue(settings.value("passgen/xxzm").isValid()?settings.value("passgen/xxzm").toInt():0);
    ui->dxzm_horizontalSlider->setValue(settings.value("passgen/dxzm").isValid()?settings.value("passgen/dxzm").toInt():0);
    ui->num_horizontalSlider->setValue(settings.value("passgen/num").isValid()?settings.value("passgen/num").toInt():0);
    ui->tsfh_horizontalSlider->setValue(settings.value("passgen/tsfh").isValid()?settings.value("passgen/tsfh").toInt():0);
    ui->exclude_lineEdit->setText(settings.value("passgen/excludeChar").isValid()?settings.value("passgen/excludeChar").toString():QString(""));
    ui->length_horizontalSlider->setValue(settings.value("passgen/passlen").isValid()?settings.value("passgen/passlen").toInt():8);

    ui->xxzm_show_label->setText(tr("%1").arg(ui->xxzm_horizontalSlider->value()));
    ui->dxzm_show_label->setText(tr("%1").arg(ui->dxzm_horizontalSlider->value()));
    ui->num_show_label->setText(tr("%1").arg(ui->num_horizontalSlider->value()));
    ui->tsfh_show_label->setText(tr("%1").arg(ui->tsfh_horizontalSlider->value()));
    ui->length_show_label->setText(tr("%1").arg(ui->length_horizontalSlider->value()));
}

void PassGenSettingDialog::loadQss()
{
    FlatUI::Instance()->setSliderQss(ui->length_horizontalSlider);
    FlatUI::Instance()->setSliderQss(ui->xxzm_horizontalSlider);
    FlatUI::Instance()->setSliderQss(ui->dxzm_horizontalSlider);
    FlatUI::Instance()->setSliderQss(ui->num_horizontalSlider);
    FlatUI::Instance()->setSliderQss(ui->tsfh_horizontalSlider);
    FlatUI::Instance()->setLineEditQss(ui->exclude_lineEdit);
    FlatUI::Instance()->setPushButtonQss(ui->ok_pushButton);
    FlatUI::Instance()->setPushButtonQss(ui->cancel_pushButton);
}

void PassGenSettingDialog::on_cancel_pushButton_clicked()
{
    close();
}
